window.onload=function(){
    var classType = location.href.slice(location.href.indexOf('/classType/') + 11);
    console.log(classType);
    classType = classType == 'micro' ? '/classType/micro' : (classType == 'free' ? '/classType/free' : '/classType/');


//$(function(){
    document.domain="lingjiuyi.cn";
    $(".header_left  .weike a").each(function () {
        if ($(this).text() == "微课") {
            $(this).addClass("select");
        } else {
            $(this).removeClass("select");
        }
    });
    var microSerch =
        "<div class='fenlei' id='direction'>" +
        "<div class='fenleiLeft'>"+
        "<p>方向:</p>"+
        "</div>"+
        "<div class='fenleiRight'>"+
        "<p class='fenleiRightList' id='jq1'>"+
        "{{if directionDataid == ''}}"+
        "<span class='active' data-id={{directionDataid}}>全部</span>"+
        "{{else}}"+
        "<span data-id=''>全部</span>"+
        "{{/if}}"+
        "{{each direction as directionValue}}"+
        "{{if directionValue.id == directionDataid}}"+
        "<span data-id='{{directionValue.id}}' class='active'>{{directionValue.course_name}}</span>"+
        "{{else}}"+
        "<span data-id='{{directionValue.id}}'>{{directionValue.course_name}}</span>"+
        "{{/if}}"+
        "{{/each}}"+
        "</p>"+
        "<p class='shousuo'>更多<img src='/home/img/micro/CombinedShapeCopy.png'/></p>"+
        "</div>"+
        "</div>";

    var wei =
        "<div class='microTitle'>"+
        "<div class='microTitleLeft' id='sort'>"+
        "{{if orderType == 0}}"+
        "<span class='active1' id='jq6' data-id='0'>综合</span>"+
        "{{else}}"+
        "<span data-id='0' id='jq6'>综合</span>"+
        "{{/if}}"+
        "{{if orderType == 1}}"+
        "<span class='active1' id='jq7' data-id='1'>最新</span>"+
        "{{else}}"+
        "<span data-id='1' id='jq7'>最新</span>"+
        "{{/if}}"+
        "{{if orderType == 2}}"+
        "<span class='active1' id='jq8' data-id='2'>最热</span>"+
        "{{else}}"+
        "<span data-id='2' id='jq8'>最热</span>"+
        "{{/if}}"+
        "</div>"+
        "<div class='microTitleRight'>"+
        "{{if totalPageCount !==1}}"+
        "<span class='pageNumbers'><span id='currentPage'>{{currentPage}}</span>/<span id='totalPageCount'>{{totalPageCount}}</span></span>"+
        "<span class='pageBtns leftBtn' style=''></span>"+
        "<span class='pageBtns rightBtn'></span>"+
        "{{else}}"+
        "<span class='pageNumbers' ><span id='currentPage'>{{currentPage}}</span>/<span id='totalPageCount'>{{totalPageCount}}</span></span>"+
        "<span class='pageBtns leftBtn' style='cursor:default ;color: #dddddd;border-color: #dddddd;'></span>"+
        "<span class='pageBtns rightBtn' style='cursor:default ;color: #dddddd;border-color: #dddddd;'></span>"+
        "{{/if}}"+
        "</div>"+
        "</div>"+
        "{{each course as courseValue}}"+
        "{{#indexHref(courseValue.description,courseValue.free,courseValue.id,courseValue.courseBelong)}}"+
        "<div class='listMicro' data-id={{courseValue.id}}>"+
        "<div class='listMicroBimgWarp'>" +
        "{{#hasImg(courseValue.cover_img)}}"+
            //"<img src='{{courseValue.smallImgPath}}' class='listMicroBimg'/>"+
        "</div>"+
        "<div class='listMicroText'>"+
        "<p class='listMicroTextTitle'>{{courseValue.lession_name}}</p>"+
        "<p class='listMicroTextList'><span>讲师：</span><span>{{courseValue.name}}</span><span class='separator'>|</span><span>{{courseValue.video_time}}</span><span>小时</span></p>"+
        "<p class='listMicroTextListMoney'><span class='xianjia'>￥{{courseValue.price}}</span><del style='color:#999;'><span class='yuanjia'>￥{{courseValue.del_price}}</span></dev><span class='renshus'><img src='/home/img/micro/renshu.png' class='renshuImg'/>{{courseValue.click_num}}</span></p>"+
        "</div>"+
        "{{ if courseValue.recommend_type == 0 }}"+
        "<img src='/home/img/micro/baopin.png' class='courseLabel'/>"+
        "{{/if }}"+
        "{{ if courseValue.recommend_type == 1 }}"+
        "<img src='/home/img/micro/jingpin.png' class='courseLabel'/>"+
        "{{/if }}"+
        "{{ if courseValue.recommend_type == 2 }}"+
        "<img src='/home/img/micro/xinpin.png' class='courseLabel'/>"+
        "{{/if }}"+
        "</div>"+
        "</a>"+
        "{{/each}}";
    var kongbai = '<div class="emptyCon clearfix">'+
        '	<img  src="/home/img/empty.png"/>'+
        '	<div class="conTxt">'+
        '		<p>暂无课程</p>'+
        '		<p>课程正在丰富中</p>'+
        '	</div>'+
        '</div>';

    var loading=
        '<div class="loadingImgWrap">'+
        '<img class="loadingImg" src="/home/img/loading.gif" alt=""/>'+
        '<p class="loadingWord">加载中</p>'+
        '</div>';


    $(".microSerchTabla").html(template.compile(loading));
    $(".microBodys").html(template.compile(loading));



    var directionheight;
    var subjectheight;
    var classifyheight;
//	列表标签
    function  listAjax(directionDataId){
        var directionDataid = directionDataId||'';
        RequestService('/course/getDirections','GET',null,function (data){
            if (data.length > 0) {
                var directionData = data;
                $(".microSerchTabla").html(template.compile(microSerch)({
                    direction:directionData,//方向数据
                    directionDataid:directionDataid,	//点击方向标签的id
                }));
                $(".fenlei").each(function(){
                    if($(this).attr("id") == "direction" && directionheight > 32){
                        $(this).height($(this).attr("data-height"));
                        $(this).find(".shousuo").html("收起<img src='/home/img/micro/CombinedShape.png'/>");
                        directionheight = 0;
                        $(this).find(".shousuo").css("display","inline-block");
                    }else if($(this).attr("id") == "subject" && subjectheight > 32){
                        $(this).height($(this).attr("data-height"));
                        $(this).find(".shousuo").html("收起<img src='/home/img/micro/CombinedShape.png'/>");
                        subjectheight = 0;
                        $(this).find(".shousuo").css("display","inline-block");
                    }else if($(this).attr("id") == "classify" && classifyheight > 32){
                        $(this).height($(this).attr("data-height"));
                        $(this).find(".shousuo").html("收起<img src='/home/img/micro/CombinedShape.png'/>");
                        classifyheight = 0;
                        $(this).find(".shousuo").css("display","inline-block");
                    }else if($(this).height()>32){
                        $(this).attr("data-height",$(this).height());
                        $(this).height(32);
                        $(this).find(".shousuo").css("display","inline-block");
                    }
                });
                $(".shousuo").click(function(){
                    if($(this).text() == '收起'){
                        $(this).parent().parent().height(32);
                        $(this).html("更多<img src='/home/img/micro/CombinedShapeCopy.png'/>");
                    }else{
                        $(this).parent().parent().height($(this).parent().parent().attr("data-height"));
                        $(this).html("收起<img src='/home/img/micro/CombinedShape.png'/>");
                    }
                });

                listTap();

            }
        })
    }


//	课程详情
    function courseAjax (directionID,subjectID,classifyID,courseLevelID,contentTypeID,orderTypeID,pageNumber){
//	console.log(directionID);
        var courseDirectionId = directionID ? '/directionId/' + directionID : '/directionId/0';
        var orderTypeID = orderTypeID ? orderTypeID : 0;
        var orderType = orderTypeID ? '/orderType/' + orderTypeID : '/orderType/' + 0;
        var PageNumber = pageNumber ? '/pageNumber/'+pageNumber : '/pageNumber/1';
        var pageSize = '/pageSize/32';
        RequestService('/course/getMicroCourses' + PageNumber + pageSize + courseDirectionId + orderType + classType,'GET',null,
            function (data){
                if (data.success !== false) {
                    var courseData = data.resultObject;  //获取到的数据
                    var currentPage = data.page.currentPage;     //当前页数
                    var totalPageCount = data.page.totalPageCount;  //总共页数
                    console.log(orderType);
                    $(".microBodys").html(template.compile(wei)({course:courseData,orderType:orderTypeID,currentPage:currentPage,totalPageCount:totalPageCount}));

                    if(courseData.length<1){
                        $(".microBodys").html(template.compile(kongbai));
                    }
                    if (data.page.totalPageCount > 1) { //分页判断
                        $(".not-data").remove();
                        $(".pages").css("display", "block");
//              $(".pages .searchPage .allPage").text(data.resultObject.totalPageCount);
                        var asd = parseInt(data.page.currentPage);
                        $("#Pagination").pagination(data.page.totalPageCount, {
                            current_page:asd-1,
                            callback: function (page) { //翻页功能
                                var directions = $("#direction .active").attr("data-id"),  //方向
                                    sorts = $("#sort .active1").attr("data-id"),		   //排序方式
                                    pages = page+1,
                                    pagesize = 32;
                                var direction = directions;
                                if(directions !==""){
                                    var direction = '/directionId/'+directions;
                                }else{
                                    var direction = '/directionId/0';
                                }
                                
//							    console.log(direction);
                                RequestService('/course/getMicroCourses' + '/pageNumber/' + pages + '/pageSize/' + pagesize+ direction + '/orderType/' +  sorts + classType,'GET',null,function (data){
//							    console.log(data);
                                    if (data.success !== false) {
                                        var courseData = data.resultObject;  //获取到的数据
                                        var currentPage = data.page.currentPage;     //当前页数
                                        var totalPageCount = data.page.totalPageCount;  //总共页数
                                        $(".microBodys div").remove();
                                        $(".microBodys").html(template.compile(wei)({course:courseData,orderType:sorts,currentPage:currentPage,totalPageCount:totalPageCount}));
                                        listTap();
                                        if(courseData.items.length<1){
                                            $(".microBodys").html(template.compile(kongbai));
                                        }
                                    }
                                })
                            }
                        });
                    } else {
                        $(".pages").css("display", "none");
                    }
                    listTap();
                }else{
                    $(".microBodys").html(template.compile(kongbai));
                }

            })
    }
//判断页面跳转
    var microFromUrl=document.referrer.toLowerCase();//判断是否是从学习空间搜索
    if(microFromUrl.indexOf(basePathStudyzone)!=-1&&window.name || microFromUrl.indexOf(basePathBlog)!=-1&&window.name){
        var wdName = JSON.parse(window.name);
        var sessionDirectionId = wdName.directionId;
        var sessionSubjectId = wdName.subjectId;
        var sessionTagId =  wdName.tagId;
        var sessionkey = sessionStorage.getItem('key');
        window.name="";//清空name的值,防止刷新缓存
    }else{
        var sessionDirectionId = sessionStorage.getItem('directionId');
        var sessionSubjectId = sessionStorage.getItem('subjectId');
        var sessionTagId =  sessionStorage.getItem('tagId');
        var sessionkey = sessionStorage.getItem('key');
    }
//	console.log(sessionkey);
    if(sessionTagId !== null && sessionSubjectId !== null && sessionTagId !== null){
        var DirectionId = 'directionId='+sessionDirectionId;
        var SubjectId = 'subjectId='+sessionSubjectId;
        var TagId = 'tagId='+sessionTagId;
        listAjax(sessionDirectionId);
        courseAjax(sessionDirectionId,sessionSubjectId,sessionTagId);
        sessionStorage.removeItem('directionId');
        sessionStorage.removeItem('subjectId');
        sessionStorage.removeItem('tagId');
    }else if(sessionkey !== null){
        RequestService("/course/matchTag",'GET',{keyWord:sessionkey},function(data){
            if (data.success === true) {
//				console.log(data.resultObject);
                var inquireDataDirectionId = data.resultObject.directionId;
                var inquireDataSubjectId = data.resultObject.subjectId;
                var inquireDataTagId = data.resultObject.tagId;
                if(inquireDataTagId==null){
                    var inquireDirectionId ='';
                    var inquireSubjectId = '';
                    var inquireTagId = '';
                }else{
                    var inquireDirectionId = 'directionId='+inquireDataDirectionId;
                    var inquireSubjectId = 'subjectId='+inquireDataSubjectId;
                    var inquireTagId = 'tagId='+inquireDataTagId;
                }
                listAjax(inquireDataDirectionId);
                courseAjax(inquireDataDirectionId,inquireDataSubjectId,inquireDataTagId);
            }
        });
        sessionStorage.removeItem('key');
    }else{
        var DirectionId = '';
        var SubjectId = '';
        var TagId='';
        listAjax();
        courseAjax();
    }

    function listTap(){
        $('#direction span').on('click',function(){
            directionheight = $("#direction").height();
            var direction = $(this).attr('data-id')
            var directionId = "directionId="+direction;
            listAjax(direction);
            courseAjax(direction);
        })
        $('#subject span').on('click',function(){
            subjectheight = $("#subject").height();
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $(this).attr('data-id');
            var subjectId = "subjectId="+subject;
            listAjax(direction,subject);
            courseAjax(direction,subject);
        })
        $('#classify span').on('click',function(){
            classifyheight = $("#classify").height();
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $(this).attr('data-id');
            listAjax(direction);
            courseAjax(direction,subject,classify);
        })
        $('#difficulty span').on('click',function(){
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $('#classify .active').attr('data-id');
            var difficulty = $(this).attr('data-id');
            listAjax(direction);
            courseAjax(direction,subject,classify,difficulty);
        })
        $('#type span').on('click',function(){
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $('#classify .active').attr('data-id');
            var difficulty = $('#difficulty .active').attr('data-id');
            var type = $(this).attr('data-id');
            listAjax(direction);
            courseAjax(direction,subject,classify,difficulty,type);
        })
        $('#sort span').on('click',function(){
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $('#classify .active').attr('data-id');
            var difficulty = $('#difficulty .active').attr('data-id');
            var type = $('#type .active').attr('data-id');
            var orderType = $(this).attr('data-id');
            listAjax(direction);
            courseAjax(direction,subject,classify,difficulty,type,orderType);
        })
        $('.rightBtn').on('click',function(){
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $('#classify .active').attr('data-id');
            var difficulty = $('#difficulty .active').attr('data-id');
            var type = $('#type .active').attr('data-id');
            var orderType = $('#sort .active1').attr('data-id');
            var currentPage = parseInt($('#currentPage').html());
            var totalPageCount = parseInt($('#totalPageCount').html());
            if(currentPage<totalPageCount){
                var currentPageS = currentPage+1;
                courseAjax(direction,subject,classify,difficulty,type,orderType,currentPageS);
                listAjax(direction);
            }else{
                var currentPageS = 	currentPage;
            }

        })
        $('.leftBtn').on('click',function(){
            var direction = $('#direction .active').attr('data-id');
            var directionId = "directionId="+direction;
            var subject = $('#subject .active').attr('data-id');
            var subjectId = "subjectId="+subject;
            var classify = $('#classify .active').attr('data-id');
            var difficulty = $('#difficulty .active').attr('data-id');
            var type = $('#type .active').attr('data-id');
            var orderType = $('#sort .active1').attr('data-id');
            var currentPage = parseInt($('#currentPage').html());
            var totalPageCount = parseInt($('#totalPageCount').html());
            if(currentPage>1){
                var currentPageS = currentPage-1;
                courseAjax(direction,subject,classify,difficulty,type,orderType,currentPageS);
                listAjax(direction);
            }else{
                var currentPageS = 	currentPage;
            }

        })
    }




};